package androidApplication.javaFiles;

import C2DMRelated.MessageHandler;
import C2DMRelated.RegistrationHandler;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

public class C2DMReceiver extends BroadcastReceiver
{
	//***************************************Class Variables*******************************************//
	private final static String tag = new String("C2DMReceiver");

	//***************************************Constructors*******************************************//	

	//***************************************Public Methods*******************************************//

	//recieving a new intent (message) and starting a new thread that will handle this intent 
	public void onReceive(final Context context, final Intent intent) 
	{
		Log.v(tag,"onReceive.........");

		final Handler handler = new Handler();

		final Runnable runable = new Runnable() 
		{
			public void run() 
			{
				handleIntent(context, intent);
			}
		};

		new Thread()
		{
			public void run() 
			{
				handler.post(runable);
			}
		}.start();
	}


	//***************************************Private Methods*******************************************//


	private void handleIntent(Context context, Intent intent)
	{
		if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
		{	
			(new RegistrationHandler()).handleRegistration(context, intent);
		}
		else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE"))
		{
			(new MessageHandler()).handleMessage(context, intent);
		}
	}


}
